package com.fakesmoker.aidawnda.importData;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

public class UserDataExcelGenerator {

    private static final String[] ROLES = {"user", "admin", "ban"};

    public static void main(String[] args) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("User Data");

        // 创建表头
        String[] headers = {"id", "userAccount", "userPassword", "unionId", "mpOpenId", "userName", "userAvatar", "userProfile", "userRole", "createTime", "updateTime", "isDelete"};
        Row headerRow = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
        }

        Random random = new Random();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        for (int rowNum = 1; rowNum <= 100; rowNum++) {
            Row row = sheet.createRow(rowNum);

            // id
            Cell idCell = row.createCell(0);
            idCell.setCellValue(rowNum);

            // userAccount
            Cell userAccountCell = row.createCell(1);
            userAccountCell.setCellValue("account" + rowNum);

            // userPassword
            Cell userPasswordCell = row.createCell(2);
            userPasswordCell.setCellValue(randomPasswordGenerator());

            // unionId
            Cell unionIdCell = row.createCell(3);
            if (random.nextBoolean()) {
                unionIdCell.setCellValue("wx" + randomStringGenerator(10));
            }

            // mpOpenId
            Cell mpOpenIdCell = row.createCell(4);
            if (random.nextBoolean()) {
                mpOpenIdCell.setCellValue("mp_" + randomStringGenerator(8));
            }

            // userName
            Cell userNameCell = row.createCell(5);
            userNameCell.setCellValue("昵称" + rowNum);

            // userAvatar
            Cell userAvatarCell = row.createCell(6);
            userAvatarCell.setCellValue("https://avatar.example.com/" + rowNum + ".jpg");

            // userProfile
            Cell userProfileCell = row.createCell(7);
            userProfileCell.setCellValue("简介" + rowNum);

            // userRole
            Cell userRoleCell = row.createCell(8);
            userRoleCell.setCellValue(ROLES[random.nextInt(ROLES.length)]);

            // createTime
            Cell createTimeCell = row.createCell(9);
            LocalDateTime now = LocalDateTime.now();
            createTimeCell.setCellValue(now.format(formatter));

            // updateTime
            Cell updateTimeCell = row.createCell(10);
            updateTimeCell.setCellValue(now.format(formatter));

            // isDelete
            Cell isDeleteCell = row.createCell(11);
            isDeleteCell.setCellValue(0);
        }

        try (FileOutputStream outputStream = new FileOutputStream("user_data.xlsx")) {
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String randomPasswordGenerator() {
        StringBuilder password = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 8; i++) {
            char c = (char) (random.nextInt(26) + 'a');
            password.append(c);
        }
        password.append('!');
        return password.toString();
    }

    private static String randomStringGenerator(int length) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append((char) (random.nextInt(26) + 'a'));
        }
        return sb.toString();
    }
}